﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this 
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;

namespace SharpGeo
{
    /// <summary>
    /// Represents an immutable polyshape
    /// </summary>
    public class PolyShape :
        Geometry,
        IEnumerable<ICoordinate>,
        IReadOnlyCoordinateCollection
    {
        #region Constructors
        /// <summary>
        /// Constructs a new polyshape
        /// </summary>
        /// <param name="exteriorRing">The exterior ring</param>
        /// <param name="interiorRings">Optional interior rings</param>
        /// <param name="shapeType">The type of shape represented by this polyshape</param>
        public PolyShape(LinearRing exteriorRing, LinearRing[] interiorRings, GeometryType shapeType)
            : this(Guid.NewGuid(), exteriorRing, interiorRings, shapeType)
        {

        }

        /// <summary>
        /// Constructs a new polyshape
        /// </summary>
        /// <param name="uniqueId">The polyshape's unique ID</param>
        /// <param name="exteriorRing">The exterior ring</param>
        /// <param name="interiorRings">Optional interior rings</param>
        /// <param name="shapeType">The type of shape represented by this polyshape</param>
        public PolyShape(
            Guid uniqueId,
            LinearRing exteriorRing,
            LinearRing[] interiorRings,
            GeometryType shapeType)
            : base(uniqueId)
        {
            //if we are passed a null/empty point list - gracefully set this to a null data type
            if (exteriorRing == null || exteriorRing.Count < 0)
            {
                ShapeType = GeometryType.Null;
                _exteriorRing = new LinearRing(new ICoordinate[0]);
            }
            else
            {
                _exteriorRing = exteriorRing;
                ShapeType = shapeType;
            }

            if (interiorRings == null)
            {
                _interiorRings = new LinearRing[0];
            }
            else
            {
                _interiorRings = interiorRings;
            }

            CalculateEnvelope();
        }

        #endregion

        #region Fields
        private LinearRing _exteriorRing;
        private LinearRing[] _interiorRings;
        #endregion

        #region Properties
        /// <summary>
        /// Gets the geometry type represented by the PolyShape
        /// </summary>
        public GeometryType ShapeType { get; protected set; }
        /// <summary>
        /// Gets the number of interior rings associated with this polyshape
        /// </summary>
        public int NumberOfInteriorRings
        {
            get { return _interiorRings.Length; }
        }

        /// <summary>
        /// Gets the exterior ring
        /// </summary>
        public LinearRing ExteriorRing { get { return _exteriorRing; } }

        /// <summary>
        /// Gets the interior rings
        /// </summary>
        public LinearRing[] InteriorRings { get { return _interiorRings; } }

        /// <summary>
        /// Gets the number of coordinates
        /// </summary>
        public int Count
        {
            get { return _exteriorRing.Count; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Returns a ICoordinate located on the exterior ring at the specified index
        /// </summary>
        /// <param name="index">The index to return the ICoordinate at</param>
        /// <returns>The ICoordinate at the specified index on the exterior ring</returns>
        public ICoordinate GetValue(int index)
        {
            return _exteriorRing.GetValue(index);
        }

        /// <summary>
        /// Calculates the envelope of this polyshape using the exterior ring
        /// </summary>
        protected void CalculateEnvelope()
        {
            //if there are no points here.. then we will just calculate an "empty" box
            if (_exteriorRing.Count < 1)
            {
                Envelope = Envelope.Empty;
                return;
            }

            Envelope = _exteriorRing.CalculateEnvelope();
        }
        #endregion

        #region IEnumerable
        IEnumerator<ICoordinate> IEnumerable<ICoordinate>.GetEnumerator()
        {
            return ((IEnumerable<ICoordinate>)_exteriorRing).GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((System.Collections.IEnumerable)_exteriorRing).GetEnumerator();
        }
        #endregion
    }
}